package com.amex.uam.util;

import java.util.Date;
import java.util.Properties;
import java.util.ResourceBundle;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class MailHelper {
	
	private static String HOST;
	private static String PORT;
	
	static {
		try {
			ResourceBundle prop = ResourceBundle.getBundle("uam_email");
			HOST = prop.getString("mail.smtp.host");
			PORT = prop.getString("mail.smtp.port");
		} catch (Throwable t) {
			System.err.println("ERROR: " + t.getMessage());
		}
	}
	public static boolean dispatchEmail(MailConf conf) {
		return dispatchEmail(conf.getFrom(), conf.getTo(), conf.getCc(), conf.getSubject(), conf.getContent());
	}
			
	public static boolean dispatchEmail(
			String from, 
			String to,
			String cc, 
			String subject, 
			String body )
	{
		
		InternetAddress fromAddress = parseAddress(from);
		InternetAddress[] toList = parseAddresses(to);
		InternetAddress[] ccList = parseAddresses(cc);
		
		return dispatchEmail(fromAddress, toList, ccList, subject, body);
	}
	
	public static InternetAddress parseAddress(String email) {
		try {
			return new InternetAddress(email, false);
		}catch(Throwable t) {
			return null;
		}
	}
	public static InternetAddress[] parseAddresses(String emails) {
		try {
			return InternetAddress.parse(emails, false);
		}catch(Throwable t) {
			return null;
		}
	}
	public static boolean dispatchEmail(
			InternetAddress from, 
			InternetAddress[] toList,
			InternetAddress[] ccList, 
			String subject, 
			String body )
	{
		
		boolean debug = true;
		
		try {
			
			Properties props = System.getProperties();
			
			if (HOST != null)
				props.put("mail.smtp.host", HOST);
			if (PORT != null)
				props.put("mail.smtp.port", PORT);

			Session session = Session.getInstance(props, null);
			
			if (debug)
				session.setDebug(true);

			Message msg = new MimeMessage(session);
			if (from != null)
				msg.setFrom(from);
			else
				msg.setFrom();

			msg.setRecipients(Message.RecipientType.TO, toList);
			if (ccList != null)
				msg.setRecipients(Message.RecipientType.CC, ccList);

			msg.setSubject(subject);
			msg.setText(body);
			msg.setSentDate(new Date());

			Transport.send(msg);

			System.out.println("\nMail was sent successfully.");
			
			return true;
			
		} catch (MessagingException mex) {
			mex.printStackTrace();
		}
		return false;
	}
	
	public static InternetAddress getInternetAddress(String emailAddress){
		try {
			return new InternetAddress(emailAddress);
		} catch (AddressException e) {
			e.printStackTrace();
			return null;
		}
	}

	public static class MailConf extends Bean{
		private String from;
		private String to;
		private String cc;
		private String subject;
		private String content;
		public String getFrom() {
			return from;
		}
		public void setFrom(String from) {
			this.from = from;
		}
		public String getTo() {
			return to;
		}
		public void setTo(String to) {
			this.to = to;
		}
		public String getCc() {
			return cc;
		}
		public void setCc(String cc) {
			this.cc = cc;
		}
		public String getSubject() {
			return subject;
		}
		public void setSubject(String subject) {
			this.subject = subject;
		}
		public String getContent() {
			return content;
		}
		public void setContent(String content) {
			this.content = content;
		}
	}
	
	public static void main(String[] args) throws Throwable  {
		MailHelper.HOST = "mail.invexer.com";
		MailHelper.PORT = "25";
		
		MailConf conf = new MailConf();
		conf.from = "kzhu@invexer.com";
		conf.to = "kyle.mstr@gmail.com";
		conf.cc = "zhukai85@gmail.com";
		conf.subject = "Test email"; 
		conf.content = "Test Content";
		
		MailHelper.dispatchEmail(conf);
		
	}
}
